{include file="common/head"/}
<style>
    .layui-form-select dl{
        z-index: 9000;
    }
</style>
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>
            <a href="javascript:location.reload()"> <i title="刷新" class="layui-icon layui-icon-refresh-3 child-page-refresh"></i></a>
            订单管理
        </legend>
    </fieldset>
    <div class="layui-form-item">
        <!-- <div class="layui-inline" style="margin-right:-10px">
            <label class="layui-form-label">订单编号</label>
            <div class="layui-inline">
                <input class="layui-input" id="key" placeholder="{:lang('pleaseEnter')}订单编号/商品名/用户昵称">
            </div>
        </div>
        <div class="layui-inline" style="margin-right:-10px">
            <label class="layui-form-label">支付时间</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" id="pay_time_begin"  autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" id="pay_time_end"  autocomplete="off" class="layui-input">
            </div>
        </div> -->
        <!-- <div class="layui-inline" style="margin-right:-10px">
            <label class="layui-form-label">订单金额</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" id="price_begin" placeholder="￥" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" id="price_end" placeholder="￥" autocomplete="off" class="layui-input">
            </div>
        </div> -->
    </div>
    <div class="layui-form">
        
        <div class="layui-inline">
            <!-- <label class="layui-form-label">订单状态</label> -->
            <div class="layui-inline">
                <select name="order_status" id="order_status"> 
                    <option value="">选择订单状态</option>
                    <option value="1">待支付</option>
                    <option value="2">待发货</option>
                    <option value="3">待收货</option>
                    <option value="4">已完成</option>
                    <option value="5">已取消</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-inline">
                <input class="layui-input" id="key" placeholder="订单/商品/用户 关键字">
            </div>
        </div>
        <!-- <div class="layui-inline">
            <label class="layui-form-label">支付时间</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" id="pay_time_begin" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" id="pay_time_end" autocomplete="off" class="layui-input">
            </div>
        </div> -->
        <button class="layui-btn" id="search" data-type="reload">{:lang('search')}</button>
    </div>
    <div style="clear: both;"></div>
    <table class="layui-table" id="list" lay-filter="list"></table>
    </div>

</div>
{include file="common/foot"/}
<script type="text/html" id="title">
   {{d.title}}{{# if(d.pic){ }}<img src="/static/admin/images/image.gif" onmouseover="layer.tips('<img src={{d.pic}}>',this,{tips: [1, '#fff']});" onmouseout="layer.closeAll();">{{# } }}
</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="list_order layui-input" value=" {{d.sort}}" size="10"/>
</script>
<script type="text/html" id="express">
    {{d.express_name}} {{d.express_num}}
</script>
<script type="text/html" id="status">
    {{# if(d.order_status == 1){ }}
    <font color="blue">待支付</font>
    {{# }else if(d.order_status == 2){ }}
    待发货
    {{# }else if(d.order_status == 3){ }}
    <font color="green">已发货</font>
    {{# }else if(d.order_status == 4){ }}
    <font color="green">已完成</font>
    {{# }else if(d.order_status == 5){ }}
    <font color="red">已取消</font>
    {{# }else if(d.order_status == 6){ }}
    <font color="red">已关闭</font>
    {{# }else if(d.order_status == 7){ }}
    已退货
    {{# }else if(d.order_status == 8){ }}
    已退款
    {{# } }}
</script>
<script type="text/html" id="pay_method">
    {{# if(d.pay_method == 1){ }}
    <font color="blue">微信支付</font>
    {{# }else if(d.pay_method == 2){ }}
    <font color="red">余额支付</font>
    {{# } }}
</script>
<script type="text/html" id="action">
    <a href="{:url('look')}?id={{d.id}}" class="layui-btn layui-btn-primary layui-btn-xs">查看</a>
    {{# if(d.order_status == 2){ }}
    <button type="button" class="layui-btn layui-btn-xs" lay-event="send">发货</button>
    {{# } }}
    {{# if(d.order_status == 1 || d.order_status == 2){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">取消订单</a>
    {{# } }}
</script>
<script>
    layui.use(['table','form','laydate'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery, laydate = layui.laydate;
        laydate.render({
            elem: '#pay_time_begin'
        });
        laydate.render({
            elem: '#pay_time_end'
        });
        var tableIn = table.render({
            id: 'ad',
            elem: '#list',
            url: '{:url("lists")}',
            method: 'post',
            totalRow: true,
            page:true,
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'id', title: '{:lang("id")}', width: 80, fixed: true, totalRowText: '合计'},
                {field: 'order_no', title: '订单编号', width: 160},
                {field: 'realname', title: '下单人姓名', width: 140},
                {field: 'goods_name', title: '购买商品', width: 300},
                {field: 'order_price', title: '订单金额',width: 120, totalRow: true},
                {field: 'order_status', title: '订单状态', width: 120, templet: '#status'},
                {field: 'pay_method', title: '支付方式', width: 120, templet: '#pay_method'},
                // {field: 'express', title: '快递信息', width: 160, templet: '#express'},
                {field: 'order_time', title: '下单时间', width: 160},
                {width: 260, align: 'center', toolbar: '#action'}
            ]],
            limit:10
        });

        //搜索
        $('#search').on('click', function () {
            var key = $('#key').val();
            var order_status = $('#order_status').val();
            var where = {key:key,order_status:order_status};
            tableIn.reload({ page: {page: 1}, where: where});
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'send'){
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post('{:url("getExpressList")}',function(res){
                    layer.close(loading);
                    if(res !== false){
                        var str = '<select class="express_company" style="margin-left: 70px;\n' +
                            '    margin-top: 34px;\n' +
                            '    width: 200px;\n' +
                            '    height: 38px;">'+
                            '<option value="rand">请选择发货快递公司</option>';
                        $.each(res,function (i,n) {
                            str += '<option value="'+n.id+'">'+n.name+'</option>';
                        });
                        str += '</select>';
                        str += '<input class="express" placeholder="请输入发货快递单号" style="margin-left: 70px;margin-top: 34px;width: 200px;height: 38px;"/>';
                        console.log(str);
                        layer.open({
                            title:'发货给买家',
                            type: 1,
                            area: ['350px', '300px'],
                            content: str,
                            btn: ['确定', '取消'],
                            yes: function(){
                                var express_company = $(".express_company").val();
                                var express = $(".express").val();
                                if(express_company && express){
                                    var set_loading = layer.load(1, {shade: [0.1, '#fff']});
                                    $.post("{:url('sendGoods')}", {id:data.id,company:express_company,express:express}, function(setRes){
                                        layer.close(set_loading);
                                        if(setRes.code > 0){
                                            layer.msg('操作成功',{time:1000,icon:1},function () {
                                                tableIn.reload();
                                            });
                                        }else{
                                            layer.msg('操作失败！',{time:1000,icon:2});
                                        }
                                    });
                                    layer.closeAll();
                                }else{
                                    layer.msg('快递公司和单号必填！',{time:1000,icon:2});
                                }
                            }
                        });
                    }else{
                        layer.msg('没有符合条件的快递公司',{time:1000,icon:2});
                    }
                })
            }
            else if (obj.event === 'refund'){
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post('{:url("getExpressList")}',function(res){
                    layer.close(loading);
                    if(res !== false){
                        var str = '<select class="express_company" style="margin-left: 70px;\n' +
                            '    margin-top: 34px;\n' +
                            '    width: 200px;\n' +
                            '    height: 38px;">'+
                            '<option value="rand">请选择退货快递公司</option>';
                        $.each(res,function (i,n) {
                            str += '<option value="'+n.id+'">'+n.name+'</option>';
                        });
                        str += '</select>';
                        str += '<input class="express" placeholder="请输入退货快递单号" style="margin-left: 70px;margin-top: 34px;width: 200px;height: 38px;"/>';
                        console.log(str);
                        layer.open({
                            title:'检测不合格，退货给给卖家',
                            type: 1,
                            area: ['350px', '300px'],
                            content: str,
                            btn: ['确定', '取消'],
                            yes: function(){
                                var express_company = $(".express_company").val();
                                var express = $(".express").val();
                                if(express_company && express){
                                    var set_loading = layer.load(1, {shade: [0.1, '#fff']});
                                    $.post("{:url('refundGoods')}", {id:data.id,company:express_company,express:express}, function(setRes){
                                        layer.close(set_loading);
                                        if(setRes.code > 0){
                                            layer.msg('操作成功',{time:1000,icon:1},function () {
                                                tableIn.reload();
                                            });
                                        }else{
                                            layer.msg('操作失败！',{time:1000,icon:2});
                                        }
                                    });
                                    layer.closeAll();
                                }else{
                                    layer.msg('快递公司和单号必填！',{time:1000,icon:2});
                                }
                            }
                        });
                    }else{
                        layer.msg('没有符合条件的快递公司',{time:1000,icon:2});
                    }
                })
            }
            if (obj.event === 'cancel'){
                layer.confirm('您确定要取消该订单吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('cancelOrder')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1},function () {
                                tableIn.reload();
                            });
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
        $('body').on('blur','.list_order',function() {
            var id = $(this).attr('data-id');
            var sort = $(this).val();
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("adOrder")}',{id:id,sort:sort},function(res){
                layer.close(loading);
                if(res.code === 1){
                    layer.msg(res.msg, {time: 1000, icon: 1});
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                }
            })
        });
        $('#delAll').click(function(){
            layer.confirm('确认要删除选中的数据吗？', {icon: 3}, function(index) {
                layer.close(index);
                var checkStatus = table.checkStatus('ad'); //test即为参数id设定的值
                var ids = [];
                $(checkStatus.data).each(function (i, o) {
                    ids.push(o.id);
                });
                if(ids==''){
                    layer.msg('请选择要删除的数据！', {time: 1000, icon: 2});
                    return false;
                }
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post("{:url('delall')}", {ids: ids}, function (data) {
                    layer.close(loading);
                    if (data.code === 1) {
                        layer.msg(data.msg, {time: 1000, icon: 1});
                        tableIn.reload();
                    } else {
                        layer.msg(data.msg, {time: 1000, icon: 2});
                    }
                });
            });
        })
    })
</script>